<?php

namespace app\merchant\model;

use think\Cache;
use app\common\model\Category as CategoryModel;

/**
 * 商品分类模型
 * Class Category
 * @package app\store\model
 */
class Category extends CategoryModel
{


    const FORM_SCENE_ADD = 'add';
    const FORM_SCENE_EDIT = 'edit';



    /**
     * 表单验证
     * @param $data
     * @return bool|false|int
     * @throws \think\exception\DbException
     */
    private function validateForm($data,$scene = self::FORM_SCENE_ADD)
    {
        if(!isset($data['name']) || empty($data['name'])){
            $this->error = '请输入分类名称';
            return false;
        }

        if(!isset($data['parent_id']) || isZeroNumeric($data['parent_id'])){
            $this->error = '上级分类参数不合法';
            return false;
        }

        if(!isset($data['sort']) || isZeroNumeric($data['sort'])){
            $this->error = '排序参数不合法';
            return false;
        }

        if(!isset($data['image_id']) || isZeroNumeric($data['image_id'])){
            $this->error = '请上传图片';
            return false;
        }

        if($scene == self::FORM_SCENE_ADD){
            $fitler = ['name' => trim($data['name']),'parent_id'=>$data['parent_id']];
            if ($this->detail($fitler)) {
                $this->error = '该分类已存在';
                return false;
            }
        }


        if($scene == self::FORM_SCENE_EDIT){
            $fitler = ['name' => trim($data['name']),'parent_id'=>$data['parent_id'],'category_id'=>['<>',$data['category_id']]];
            if ($this->detail($fitler)) {
                $this->error = '该分类已存在';
                return false;
            }
        }

        return true;
    }

    /**
     * 查找分类详情
     * @param $data
     * @return false|int
     */
    public function detail($fitler)
    {
        return $this->where($fitler)->find();
    }


    /**
     * 添加新记录
     * @param $data
     * @return false|int
     */
    public function add($data)
    {
        // 表单验证
        if (!$this->validateForm($data,self::FORM_SCENE_ADD)) {
            return false;
        }

        $data['wxapp_id'] = self::$wxapp_id;
        $this->deleteCache();
        return $this->allowField(true)->save($data);
    }

    /**
     * 编辑记录
     * @param $data
     * @return bool|int
     */
    public function edit($data)
    {
        // 表单验证
        if (!$this->validateForm($data,self::FORM_SCENE_EDIT)) {
            return false;
        }

        // 验证：一级分类如果存在子类，则不允许移动
        if ($data['parent_id'] > 0 && static::hasSubCategory($this['category_id'])) {
            $this->error = '该分类下存在子分类，不可以移动';
            return false;
        }
        $this->deleteCache();

        return $this->allowField(true)->save($data) !== false;
    }

    /**
     * 删除商品分类
     * @param $categoryId
     * @return bool|int
     */
    public function remove($categoryId)
    {
        // 判断是否存在商品
        if ($goodsCount = (new Goods)->getGoodsTotal(['category_id' => $categoryId])) {
            $this->error = '该分类下存在' . $goodsCount . '个商品，不允许删除';
            return false;
        }
        // 判断是否存在子分类
        if (static::hasSubCategory($categoryId)) {
            $this->error = '请先删除子分类';
            return false;
        }
        $this->deleteCache();
        return $this->delete();
    }

    /**
     * 删除缓存
     * @return bool
     */
    private function deleteCache()
    {
        return Cache::rm('category_' . static::$wxapp_id);
    }

}
